~ chicken-core (master) /manual/Module (chicken condition)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken condition)56This module provides various procedures and special forms for raising7and handling exceptions with "condition objects". Condition objects8provide a structured and composable way to encode the kind of9exception that took place, and provide the necessary context.1011CHICKEN's exception handling is based on the12[[http://srfi.schemers.org/srfi-12/srfi-12.html|SRFI-12]] exception13system. This document contains the core of the SRFI-12 spec as well14as CHICKEN implementation specifics.1516There is also a {{srfi-12}} or {{(srfi 12)}} module which only17includes the standard procedures and macros from the SRFI document,18without the CHICKEN extensions. {{(chicken condition)}} offers the19complete set of procedures and macros, both CHICKEN-specific and20standard SRFI-12.212223== CHICKEN implementation2425=== System conditions2627All error-conditions signaled by the system are of kind {{exn}}.28The following composite conditions are additionally defined:2930<table>3132<tr><td> (exn arity) </td><td>3334Signaled when a procedure is called with the wrong number of arguments.3536</td></tr><tr><td> (exn type) </td><td>3738Signaled on type-mismatch errors, for example when an argument of the wrong39type is passed to a built-in procedure.4041</td></tr><tr><td> (exn arithmetic) </td><td>4243Signaled on arithmetic errors, like division by zero.4445</td></tr><tr><td> (exn i/o) </td><td>4647Signaled on input/output errors.4849</td></tr><tr><td> (exn i/o file) </td><td>5051Signaled on file-related errors.5253</td></tr><tr><td> (exn i/o net) </td><td>5455Signaled on network errors.5657</td></tr><tr><td> (exn bounds) </td><td>5859Signaled on errors caused by accessing non-existent elements of a collection.6061</td></tr><tr><td> (exn runtime) </td><td>6263Signaled on low-level runtime-system error-situations.6465</td></tr><tr><td> (exn runtime limit) </td><td>6667Signaled when an internal limit is exceeded (like running out of memory).6869</td></tr><tr><td> (exn match) </td><td>7071Signaled on errors raised by failed matches (see the section on {{match}}).7273</td></tr><tr><td> (exn syntax) </td><td>7475Signaled on syntax errors.7677</td></tr>7879</table>8081=== Notes8283* All error-exceptions (of the kind {{exn}}) are non-continuable.8485* Error-exceptions of the {{exn}} kind have additional {{arguments}} and86{{location}} properties that contain the arguments passed to the87exception-handler and the name of the procedure where the error occurred (if88available).8990* Within the interpreter (csi), a user-interrupt ({{signal/int}}) signals an91exception of the kind {{user-interrupt}}.9293* The procedure {{condition-property-accessor}} accepts an optional third94argument. If the condition does not have a value for the desired property and95if the optional argument is given, no error is signaled and the accessor96returns the third argument.9798* On platforms that support the {{sigprocmask(3)}} POSIX API function,99the signals {{SIGSEGV}}, {{SIGFPE}}, {{SIGBUS}} and {{SIGILL}} will be100caught and trigger an exception instead of aborting the process, if101possible. If the unwinding and handling of the signal raises one of102these signals once again, the process will abort with an error103message.104105* New in CHICKEN 5.4.0: condition objects produced by procedures that106change errno have an {{errno}} property. To access it, use107{{(get-condition-property <the-condition-object> 'exn 'errno)}}.108109=== Additional API110111==== condition-case112113<macro>(condition-case EXPRESSION CLAUSE ...)</macro>114115Evaluates {{EXPRESSION}} and handles any exceptions that are covered by116{{CLAUSE ...}}, where {{CLAUSE}} should be of the following form:117118 CLAUSE = ([VARIABLE] (KIND ...) BODY ...)119120If provided, {{VARIABLE}} will be bound to the signaled exception121object. {{BODY ...}} is executed when the exception is a property-122or composite condition with the kinds given {{KIND ...}} (unevaluated).123If no clause applies, the exception is re-signaled in the same dynamic124context as the {{condition-case}} form.125126<enscript highlight=scheme>127(define (check thunk)128 (condition-case (thunk)129 [(exn file) (print "file error")]130 [(exn) (print "other error")]131 [var () (print "something else")] ) )132133(check (lambda () (open-input-file ""))) ; -> "file error"134(check (lambda () some-unbound-variable)) ; -> "othererror"135(check (lambda () (signal 99))) ; -> "something else"136137(condition-case some-unbound-variable138 ((exn file) (print "ignored")) ) ; -> signals error139</enscript>140141==== get-condition-property142143<procedure>(get-condition-property CONDITION KIND PROPERTY [DEFAULT])</procedure>144145A slightly more convenient condition property accessor, equivalent to146147 ((condition-property-accessor KIND PROPERTY [DEFAULT]) CONDITION)148149==== condition150151<procedure>(condition LST1 LST2 ...)</procedure>152153This is a more convenient constructor for conditions. Each of154{{LST1}}, {{LST2}} etc is a list of the following form:155156 (KIND PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...)157158In other words, the following:159160 (signal (condition '(exn location foo message "hi") '(file bar 1)))161162is equivalent to the SRFI-12 code:163164 (signal (make-composite-condition165 (make-property-condition 'exn 'location 'foo 'message "hi")166 (make-property-condition 'file 'bar 2)))167168169==== condition->list170171<procedure>(condition->list CONDITION)</procedure>172173This procedure converts a condition object into a list holding all the174conditions that are represented by the ''CONDITION'' object. It is175formatted as follows:176177 ((KIND1 PROPERTY1 VALUE1 PROPERTY2 VALUE2 ...) (KIND2 ... ) ... )178179There is no guaranteed order within the list.180181182==== print-error-message183184<procedure>(print-error-message EXN [PORT [HEADER]])</procedure>185186Prints an appropriate error message to {{PORT}} (which defaults to the187value of {{(current-output-port)}} for the object {{EXN}}. {{EXN}} may188be a condition, a string or any other object. The output is prefixed189by the {{HEADER}}, which defaults to {{"Error:"}}.190191192193== SRFI-12 specification194195A Scheme implementation ("the system") raises an exception whenever an196error is to be signaled or whenever the system determines that evaluation197cannot proceed in a manner consistent with the semantics of Scheme. A198program may also explicitly raise an exception.199200Whenever the system raises an exception, it invokes the current exception201handler with a condition object (encapsulating information about the202exception) as its only argument. Any procedure accepting one argument203may serve as an exception handler. When a program explicitly raises an204exception, it may supply any object to the exception handler.205206An exception is either continuable or non-continuable. When the current207exception handler is invoked for a continuable exception, the continuation208uses the handler's result(s) in an exception-specific way to continue.209When an exception handler is invoked for a non-continuable exception,210the continuation raises a non-continuable exception indicating that the211exception handler returned. On CHICKEN, system error exceptions212(of kind {{exn}}) are non-continuable.213214=== Exception Handlers215216==== current-exception-handler217218<parameter>(current-exception-handler [PROCEDURE])</parameter><br>219220Sets or returns the current exception handler, a procedure of one221argument, the exception object.222223==== with-exception-handler224225<procedure>(with-exception-handler handler thunk)</procedure><br>226227Returns the result(s) of invoking ''thunk''. The ''handler'' procedure228is installed as the current exception handler in the dynamic context of229invoking ''thunk''.230231Example:232233<enscript highlight=scheme>234(call-with-current-continuation235 (lambda (k)236 (with-exception-handler (lambda (x) (k '()))237 (lambda () (car '())))))238;=> '()239</enscript>240241Note that the handler procedure must somehow return non-locally out of242the dynamic extent of the {{with-exception-handler}} form, because243returning normally will signal yet another exception and thus result244in non-termination.245246==== handle-exceptions247248<macro>(handle-exceptions var handle-expr expr1 expr2 ...)</macro><br>249250Evaluates the body expressions ''expr1'', ''expr2'', ... in sequence with251an exception handler constructed from ''var'' and ''handle-expr''. Assuming252no exception is raised, the result(s) of the last body expression is(are)253the result(s) of the {{handle-exceptions}} expression.254255The exception handler created by {{handle-exceptions}} restores the dynamic256context (continuation, exception handler, etc.) of the {{handle-exceptions}}257expression, and then evaluates ''handle-expr'' with ''var'' bound to the258value provided to the handler.259260Examples:261262<enscript highlight=scheme>263(handle-exceptions exn264 (begin265 (display "Went wrong")266 (newline))267 (car '()))268; displays "Went wrong"269270(handle-exceptions exn271 (cond272 ((eq? exn 'one) 1)273 (else (abort exn)))274 (case (random 3)275 [(0) 'zero]276 [(1) (abort 'one)]277 [else (abort "Something else")]))278;=> 'zero, 1, or (abort "Something else")279</enscript>280281=== Raising Exceptions282283==== abort284285<procedure>(abort obj)</procedure><br>286287Raises a non-continuable exception represented by ''obj''. The {{abort}}288procedure can be implemented as follows:289290<enscript highlight=scheme>291(define (abort obj)292 ((current-exception-handler) obj)293 (abort (make-property-condition294 'exn295 'message296 "Exception handler returned")))297</enscript>298299The {{abort}} procedure does not ensure that its argument is a condition.300If its argument is a condition, {{abort}} does not ensure that the condition301indicates a non-continuable exception.302303==== signal304305<procedure>(signal obj)</procedure><br>306307Raises a continuable exception represented by ''obj''. The {{signal}} procedure308can be implemented as follows:309310<enscript highlight=scheme>311(define (signal exn)312 ((current-exception-handler) exn))313</enscript>314315The {{signal}} procedure does not ensure that its argument is a condition.316If its argument is a condition, {{signal}} does not ensure that the condition317indicates a continuable exception.318319=== Condition Objects320321==== condition?322323<procedure>(condition? obj)</procedure><br>324325Returns #t if ''obj'' is a condition, otherwise returns #f.326327Rationale: Any Scheme object may be passed to an exception handler. This328would cause ambiguity if conditions were not disjoint from all of Scheme's329standard types.330331==== make-property-condition332333<procedure>(make-property-condition kind-key prop-key value ...)</procedure><br>334335This procedure accepts any even number of arguments after ''kind-key'',336which are regarded as a sequence of alternating ''prop-key'' and ''value''337objects. Each ''prop-key'' is regarded as the name of a property, and338each ''value'' is regarded as the value associated with the ''key'' that339precedes it. Returns a ''kind-key'' condition that associates the given340''prop-key''s with the given ''value''s.341342==== make-composite-condition343344<procedure>(make-composite-condition condition ...)</procedure><br>345346Returns a newly-allocated condition whose components correspond to the347given ''condition''s. A predicate created by {{condition-predicate}} returns348true for the new condition if and only if it returns true for one or more349of its component conditions.350351==== condition-predicate352353<procedure>(condition-predicate kind-key)</procedure><br>354355Returns a predicate that can be called with any object as its argument.356Given a condition that was created by {{make-property-condition}}, the357predicate returns #t if and only if ''kind-key'' is EQV? to the kind key358that was passed to {{make-property-condition}}. Given a composite condition359created with {{make-composite-condition}}, the predicate returns #t if and only360if the predicate returns #t for at least one of its components.361362==== condition-property-accessor363364<procedure>(condition-property-accessor kind-key prop-key [default])</procedure><br>365366Returns a procedure that can be called with any condition that satisfies367{{(condition-predicate ''kind-key'')}}. Given a condition that was created368by {{make-property-condition}} and ''kind-key'', the procedure returns the369value that is associated with ''prop-key''. Given a composite condition370created with {{make-composite-condition}}, the procedure returns the value that371is associated with ''prop-key'' in one of the components that satisfies372{{(condition-predicate ''kind-key'')}}.373374On CHICKEN, this procedure accepts an optional third argument375DEFAULT. If the condition does not have a value for the desired376property and if the optional argument is given, no error is signaled377and the accessor returns the third argument.378379When the system raises an exception, the condition it passes to the380exception handler includes the {{'exn}} kind with the following381properties:382383; message : the error message384; arguments: the arguments passed to the exception handler385; location: the name of the procedure where the error occurred (if available)386387Thus, if ''exn'' is a condition representing a system exception,388then389390<enscript highlight=scheme>391 ((condition-property-accessor 'exn 'message) exn)392</enscript>393394extracts the error message from ''exn''. Example:395396<enscript highlight=scheme>397(handle-exceptions exn398 (begin399 (display "Went wrong: ")400 (display401 ((condition-property-accessor 'exn 'message) exn))402 (newline))403 (car '()))404; displays something like "Went wrong: can't take car of nil"405</enscript>406407=== More Examples408409<enscript highlight=scheme>410(define (try-car v)411 (let ((orig (current-exception-handler)))412 (with-exception-handler413 (lambda (exn)414 (orig (make-composite-condition415 (make-property-condition416 'not-a-pair417 'value418 v)419 exn)))420 (lambda () (car v)))))421422(try-car '(1))423;=> 1424425(handle-exceptions exn426 (if ((condition-predicate 'not-a-pair) exn)427 (begin428 (display "Not a pair: ")429 (display430 ((condition-property-accessor 'not-a-pair 'value) exn))431 (newline))432 (abort exn))433 (try-car 0))434; displays "Not a pair: 0"435436(let* ((cs-key (list 'color-scheme))437 (bg-key (list 'background))438 (color-scheme? (condition-predicate cs-key))439 (color-scheme-background440 (condition-property-accessor cs-key bg-key))441 (condition1 (make-property-condition cs-key bg-key 'green))442 (condition2 (make-property-condition cs-key bg-key 'blue))443 (condition3 (make-composite-condition condition1 condition2)))444 (and (color-scheme? condition1)445 (color-scheme? condition2)446 (color-scheme? condition3)447 (color-scheme-background condition3)))448; => 'green or 'blue449</enscript>450451----452Previous: [[Module (chicken bytevector)]]453454Next: [[Module (chicken continuation)]]